﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using NmbpProject2.BusinessLayer;
using NmbpProject2.DboClasses;

namespace NmbpProject2.PresentationLayer
{
    public partial class LanguagesForm : Form
    {
        public LanguagesForm()
        {
            InitializeComponent();
            refreshDataSource();
        }

        /// <summary>
        /// Dohvaća popis svih ljudi. Listu svih ljudi povezuje
        /// kao izvor podataka za dataGridView na formi.
        /// </summary>
        private void refreshDataSource()
        {
            ProgLanguageBL pBL = new ProgLanguageBL();
            List<ProgLanguage> allLangs = pBL.getAllLangs();
            dataGridViewLanguages.DataSource = allLangs;
            dataGridViewLanguages.Refresh();
            pBL = null;
        }

        /// <summary>
        /// Metoda koja se aktivira klikom na gumb za brisanje označenog jezika.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDeleteLanguage_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewLanguages.SelectedRows;
            if (selectedRows.Count > 0)
            {
                List<ProgLanguage> langs = new List<ProgLanguage>();
                ProgLanguageBL pBL = new ProgLanguageBL();
                for (int i = 0; i < selectedRows.Count; i++)
                {
                    langs.Add((ProgLanguage) selectedRows[i].DataBoundItem);
                }
                if (MessageBox.Show(
                        "Are you sure you want to delete selected languages?",
                        "Confirm delete",
                        MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        pBL.delete(langs, true);
                    }
                    catch (Exception exception)
                    {
                        string message = String.Concat(
                            exception.Message,
                            "\n\nAre you sure you want to continue?"
                            );
                        if (MessageBox.Show(message, "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            pBL.delete(langs, false);
                        }
                    }
                }
                refreshDataSource();
            }
        }

        /// <summary>
        /// Metoda koja se aktivira klikom  na gumb za uređivanje označenog jezika.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEditLanguage_Click_1(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewLanguages.SelectedRows;
            if (selectedRows.Count == 1)
            {
                ProgLanguage language = (ProgLanguage) selectedRows[0].DataBoundItem;
                if (validateInput(txtEditName.Text))
                {
                    ProgLanguageBL pBL = new ProgLanguageBL();
                    try
                    {
                        pBL.update(language, txtEditName.Text);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                    }
                    refreshDataSource();
                }
            }
        }

        /// <summary>
        /// Metoda koja se aktivira klikom na gumb za dodavanje novog jezika.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAddLanguage_Click_1(object sender, EventArgs e)
        {
            if (validateInput(txtAddName.Text))
            {
                ProgLanguageBL pBL = new ProgLanguageBL();
                try
                {
                    pBL.insertLanguage(txtAddName.Text);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
                refreshDataSource();
                txtAddName.Text = "";
            }
        }

        /// <summary>
        /// Metoda koja popuni text polja za uređivanje podataka s podacima 
        /// o trenutno označenom jeziku.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewLanguages_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewLanguages.SelectedRows;
            if (selectedRows.Count == 1)
            {
                ProgLanguage language = (ProgLanguage) selectedRows[0].DataBoundItem;
                txtEditName.Text = language.Name;
            }
            else
            {
                txtEditName.Text = "";
            }
        }

        private bool validateInput(string Name)
        {
            bool valid = true;
            try
            {
                if (Name.Length == 0)
                {
                    throw new Exception("Name cannot be empty!");
                }
            }
            catch (Exception exception)
            {
                valid = false;
                MessageBox.Show(exception.Message);
            }
            return valid;
        }

        private void dataGridViewLanguages_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }
    }
}